﻿#define  _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"

void SListPrint(SList* L)
{
	assert(L);
	for (int i = 0; i < L->size; i++)
	{
		printf("%d", L->arr[i]);
	}
	printf("\n");
}
void SListInit(SList* L)
{
	assert(L);
	L->arr = NULL;
	L->size = L->capacity = 0;
}
void SListDestroy(SList* L)
{
	assert(L);
	if (L->arr)
	{
		free(L->arr);
		L->arr = NULL;
		L->capacity = L->size = NULL;
	}
}
void SListCheckCapacity(SList* L)
{
	assert(L);
	if (L->capacity == L->size)
	{
		int newcapacity = L->capacity == 0 ? 4 : L->capacity * 2;
		DataType* tmp = (DataType*)realloc(L->arr, newcapacity * sizeof(DataType));
		if (tmp == NULL)
		{
			printf("realloc is fail\n");
			return;
		}
		L->arr = tmp;
		L->capacity = newcapacity;
	}
}
void SListPushBack(SList* L, DataType x)
{
	assert(L);
	SListCheckCapacity(L);
	L->arr[L->size] = x;
	L->size++;
}
void SListPushFront(SList* L, DataType x)
{
	assert(L);
	SListCheckCapacity(L);
	int end = L->size - 1;
	while (end >= 0)
	{
		L->arr[end + 1] = L->arr[end];
		--end;
	}
	L->arr[0] = x;
	L->size++;
}
void SListPopFront(SList* L)
{
	assert(L);
	assert(L->size > 0);
	int begin = 1;
	while (begin < L->size)
	{
		L->arr[begin - 1] = L->arr[begin];
		++begin;
	}
	L->size--;
}
void SListPopBack(SList* L)
{
	assert(L);
	assert(L->size > 0);
	L->size--;
}
void SListInsert(SList* L, int pos, DataType x)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	SListCheckCapacity(L);
	int end = L->size - 1;
	while (end >= pos)
	{
		L->arr[end + 1] = L->arr[end];
		--end;
	}
	L->arr[pos] = x;
	L->size++;
}
void SListErase(SList* L,int pos)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	int begin = pos + 1;
	while (begin < L->size)
	{
		L->arr[begin - 1] = L->arr[begin];
		++begin;
	}
	L->size--;
}
int SListFind(SList* L,DataType x)
{
	assert(L);
	for (int i = 0; i < L->size; i++)
	{
		if (L->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void SListModify(SList* L, int pos, DataType x)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	L->arr[pos] = x;
}​;

//-----------------------------------------------------------------------
//---------------------------------------------------------------------------------

void SListPrint(SList* L)
{
	assert(L);
	for (int i = 0; i < L->size; i++)
	{
		printf("%d", L->arr[i]);
	}
	printf("\n");
}
void SListInit(SList* L)
{
	assert(L);
	L->arr = NULL;
	L->size = L->capacity = 0;
}
void SListDestroy(SList* L)
{
	assert(L);
	free(L->arr);
	L->arr = NULL;
	L->capacity = L->size = 0;
}
void SListCheckCapacity(SList* L)
{
	assert(L);
	if (L->capacity == L->size)
	{
		int newcapacity = L->capacity == 0 ? 4 : L->capacity * 2;
		DataType* tmp = (DataType*)realloc(L->arr, sizeof(DataType) * newcapacity);
		if (tmp == NULL)
		{
			printf("realloc is fail\n");
			exit(-1);
		}
		L->capacity = newcapacity;
		L->arr = tmp;
	}
}
void SListPushBack(SList* L, DataType x)
{
	assert(L);
	SListCheckCapacity(L);
	L->arr[L->size] = x;
	L->size++;
}
void SListPushFront(SList* L, DataType x)
{
	assert(L);
	SListCheckCapacity(L);
	int end = L->size - 1;
	while (end >= 0)
	{
		L->arr[end + 1] = L->arr[end];
		--end;
	}
	L->arr[0] = x;
	L->size++;
}
void SListPopFront(SList* L)
{
	assert(L);
	assert(L->size > 0);
	int begin = 1;
	while (begin < L->size)
	{
		L->arr[begin - 1] = L->arr[begin];
		++begin;
	}
	L->size--;
}
void SListPopBack(SList* L)
{
	assert(L);
	assert(L->size > 0);
	L->size--;
}
void SListInsert(SList* L, int pos, DataType x)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	int end = L->size - 1;
	while (end >= pos)
	{
		L->arr[end + 1] = L->arr[end];
		--end;
	}
	L->arr[pos] = x;
	L->size++;
}
void SListErase(SList* L, int pos)
{
	assert(L);
	assert(pos >= 0 && pos <= L->size);
	int begin = pos + 1;
	while (begin < L->size)
	{
		L->arr[begin - 1] = L->arr[begin];
		++begin;
	}
	L->size--;
}
int SListFind(SList* L, DataType x)
{
	assert(L);
	for (int i = 0; i < L->size; i++)
	{
		if (L->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void SListModify(SList* L, int pos, DataType x)
{
	assert(L);
	assert(pos>=0 && pos<=L->size);
	L->arr[pos] = x;
}
